import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:panda/base/Constant.dart';
import 'package:panda/entity/coupon.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/util/AppSaveInfoUtil.dart';
import 'package:panda/util/DateUtils.dart';
import 'package:panda/util/MySeparator.dart';
import 'package:panda/util/SignUtil.dart';

class MyCouponSubPage extends StatefulWidget {
  int type;
  MyCouponSubPage({Key key, @required this.type}) : super(key: key);

  @override
  _MyCouponSubPageState createState() => _MyCouponSubPageState();
}

class _MyCouponSubPageState extends State<MyCouponSubPage> {
  EasyRefreshController _controller = new EasyRefreshController();
  String token;
  int page = 1;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getString(TOKEN).then((value){
token=value;
getCouponsList();
    });
  }
List<UserCouponListDataData> dataList = new List();

  getCouponsList() async {
    Map map = new Map();
    map['token'] = token;
    map['p']='1';
    map['per']=100;
    // map['sign_type'] = "dmshop.UserCouponsRecord.getList";
    Response<String> response = await HttpUtil().post(
        Api.BaseUrl + "/index.php/wap/UserCouponsRecord/getList",
        data: getSign(map));
    Map userMap = json.decode(response.data);
    UserCouponListEntity homeListEntity =
        new UserCouponListEntity.fromJson(userMap);
    if (userMap['code'] == 1 && mounted) {
      dataList.addAll(homeListEntity.data.datas);
      setState(() {});
    }
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      child: EasyRefresh(
        controller: _controller,
        enableControlFinishLoad: true,
        enableControlFinishRefresh: true,
        header: MaterialHeader(),
        footer: MaterialFooter(),
        // onLoad: () {},
        // onRefresh: () {

        // },
        child: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return renderRow(context, index);
          },
          itemCount: dataList.length,
        ),
      ),
    );
  }

  Widget renderRow(BuildContext context, int index) {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil.getInstance().setHeight(DP_20),
          ScreenUtil.getInstance().setHeight(DP_20),
          ScreenUtil.getInstance().setHeight(DP_20),
          0),
      // padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
      decoration: BoxDecoration(
          color: white,
          borderRadius:
              BorderRadius.circular(ScreenUtil.getInstance().setHeight(DP_10))),
      child: Stack(
        children: <Widget>[
          Padding(
            padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
            child: Row(
              children: <Widget>[
                Column(
                  children: <Widget>[
                    Text(MONEY + dataList[index].money,
                        style: TextStyle(
                            fontSize: ScreenUtil.getInstance()
                                .setSp(BIG_FONT_SIZE * 2 - 15),
                            color:
                                widget.type == 0 ? mainColor : Colors.black45)),
                    Container(
                      height: ScreenUtil.getInstance().setWidth(DP_8),
                    ),
                    Text( dataList[index].name,
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(MINI_FONT_SIZE+3),
                            color: black_99)),
                  ],
                ),
                Container(
                  width: ScreenUtil.getInstance().setWidth(50),
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('有效日期',
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                            color: widget.type == 0 ? black : Colors.black45)),
                    Container(
                      height: ScreenUtil.getInstance().setWidth(DP_15),
                    ),
                    Text(dataList[index].use_start_time+" - "+dataList[index].use_end_time,
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(MINI_FONT_SIZE),
                            color:
                                widget.type == 0 ? black_99 : Colors.black45)),
                  ],
                ),
              ],
            ),
          ),
          widget.type == 0
              ? Positioned(
                  child:(DateUtils.instance.getTimeStap(formartData: dataList[index].use_end_time)- DateTime.now().millisecondsSinceEpoch)>60*24*3*60*1000?Center():Container(
                    padding: EdgeInsets.fromLTRB(
                        ScreenUtil.getInstance().setHeight(DP_20),
                        4,
                        ScreenUtil.getInstance().setHeight(DP_20),
                        4),
                    decoration: BoxDecoration(
                      color: Colors.orange[300],
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(
                              ScreenUtil.getInstance().setHeight(DP_10)),
                          topRight: Radius.circular(
                              ScreenUtil.getInstance().setHeight(DP_10))),
                    ),
                    child: Text('即将过期',
                        style: TextStyle(
                            color: white,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(MINI_FONT_SIZE))),
                  ),
                  right: 0,
                )
              : Positioned(
                  right: ScreenUtil.getInstance().setHeight(DP_20),
                  top: 10,
                  bottom: 10,
                  child: widget.type == 1
                      ? Image.asset(
                          "images/coupon_used.png",
                          scale: 1.2,
                        )
                      : Image.asset(
                          "images/coupon_expried.png",
                          scale: 1.2,
                        ))
        ],
      ),
    );
  }
}
